Total Complexity | 8 |
Total Lines | 52 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import { EventType } from './Event.entity'; |
||
4 | |||
5 | export class GetFairCalendarOverview { |
||
6 | public index(items: ICalendar[]): ICalendarOverview { |
||
7 | const itemsByDate = []; |
||
8 | const overview: ICalendarOverview = { |
||
9 | mission: 0, |
||
10 | dojo: 0, |
||
11 | formationConference: 0, |
||
12 | leave: 0, |
||
13 | support: 0, |
||
14 | other: 0, |
||
15 | mealTicket: 0 |
||
16 | }; |
||
17 | |||
18 | for (const item of items) { |
||
19 | const dayIndex = new Date(item.getDate()).getDate() - 1; |
||
20 | const time = item.getTime() / 100; |
||
21 | const type = item.getType(); |
||
22 | |||
23 | if (itemsByDate[dayIndex]) { |
||
24 | itemsByDate[dayIndex].push({time, type}); |
||
25 | } else { |
||
26 | itemsByDate[dayIndex] = [{time, type}]; |
||
27 | } |
||
28 | |||
29 | overview[item.getType()] += time; |
||
30 | } |
||
31 | |||
32 | return { |
||
33 | ...overview, |
||
34 | mealTicket: this.getNumberOfMealTickets(Object.values(itemsByDate)) |
||
35 | }; |
||
36 | } |
||
37 | |||
38 | public getNumberOfMealTickets(itemsByDate: any[]): number { |
||
39 | let mealTicket = 0; |
||
40 | |||
41 | for (const sortedEvent of itemsByDate) { |
||
42 | let totalPerDay = 0; |
||
43 | |||
44 | for (const { time, type } of sortedEvent) { |
||
45 | if (type !== EventType.OTHER) { |
||
46 | totalPerDay += time; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | if (totalPerDay > 0.5) { |
||
51 | mealTicket++; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | return mealTicket; |
||
56 | } |
||
58 |